require(["./config"], () => {
  require(["template", "cookie"], (template, cookie) => {
    class login {
      constructor() {
        this.Logins();
      }
      Logins() {
        $("#btn").on("click", function() {
          let username = $("#username").val();
          let password = $("#password").val();
          let users = localStorage.getItem("locaL");
          // console.log(localStorage.getItem("locaL"));
          if (users) {
            users = JSON.parse(users);

            const isExist = users.some(users => {
              return users.username === username && users.password === password;
            });
            if (isExist) {
              alert("登录成功，即将跳转首页");
              // TODO: 7天免登陆
              $.cookie("loginUser", JSON.stringify({ username, password }), {
                path: "/"
              });
              window.location.replace("/");
            } else {
              alert("用户名或者密码错误");
            }
          } else {
            alert("请先注册");
            window.location.href = "register.html";
          }
        });
      }
    }
    new login();
  });
});
